local addonName, IWonew = ...

local regantItems = {}


local f = CreateFrame("Frame")
-- /dump C_PlayerInteractionManager.IsInteractingWithNpcOfType(Enum.PlayerInteractionType.AccountBanker)
-- /dump C_GuildBank.IsGuildBankEnabled()
function f:ITEM_SEARCH_RESULTS_UPDATED(event, itemKey)
	for i = 1, C_AuctionHouse.GetNumItemSearchResults(itemKey) do
		local result = C_AuctionHouse.GetItemSearchResultInfo(itemKey, i)
    if result then
      print(event, itemKey.itemID, i, result.auctionID, result.buyoutAmount)
    end
		
	end
end

function f:COMMODITY_SEARCH_RESULTS_UPDATED(event, itemID)
  local averagePrice = 0
  for i = 1, 3 do
    local result = C_AuctionHouse.GetCommoditySearchResultInfo(itemID, i)
    local unitPrice = result and result.unitPrice or 0
    averagePrice = averagePrice + unitPrice
  end
  averagePrice = math.ceil(averagePrice / 3)
  local cachedItem = IWonew.getListItem(regantItems, "itemID", itemID)
  if cachedItem and cachedItem.unitPrice == nil then
    cachedItem.unitPrice = averagePrice
    print("扫描结果", averagePrice)
    local unQueryItems = IWonew.filterTable(regantItems, function(item)
      return item.unitPrice == nil
    end)
    if #regantItems > 0 and #unQueryItems <= 0 then
      print("扫描完毕，开始计算利润")
      ComputeProfit()
    end
  end
	-- for i = 1, C_AuctionHouse.GetNumCommoditySearchResults(itemID) do
	-- 	local result = C_AuctionHouse.GetCommoditySearchResultInfo(itemID, i)
  --   if result then
  --     print(event, itemID, i, result.quantity, result.auctionID, result.unitPrice)
  --   end
	-- end
end

function f:OnEvent(event, ...)
	self[event](self, event, ...)
end

f:RegisterEvent("ITEM_SEARCH_RESULTS_UPDATED")
f:RegisterEvent("COMMODITY_SEARCH_RESULTS_UPDATED")
f:SetScript("OnEvent", f.OnEvent)

function ComputeProfit()
  for i = 1, #TargetEnchantingItems do
    local curPrice = IWonew.getListItem(regantItems, "itemID", TargetEnchantingItems[i].itemID).unitPrice
    local regantPrice = 0
    for j = 1, #TargetEnchantingItems[i].regants do
      local quality = TargetEnchantingItems[i].regants[j].quality
      local unitPrice = IWonew.getListItem(regantItems, "itemID", TargetEnchantingItems[i].regants[j].itemID).unitPrice
      regantPrice = regantPrice + quality * unitPrice
    end
    print(TargetEnchantingItems[i].name .. "价格:" .. curPrice / 10000 .. "材料价格：" .. regantPrice / 10000 .. "利润：" .. (curPrice - regantPrice) / 10000)
  end
end

function SetRegantItems()
  if #regantItems > 0 then return end
  for i = 1, #TargetEnchantingItems do
    table.insert(regantItems, {name = TargetEnchantingItems[i].name, itemID = TargetEnchantingItems[i].itemID, unitPrice = nil})
    for j = 1, #TargetEnchantingItems[i].regants do
      if not IWonew.getListItem(regantItems, "itemID", TargetEnchantingItems[i].regants[j].itemID) then
        table.insert(regantItems, {name = TargetEnchantingItems[i].regants[j].name, itemID = TargetEnchantingItems[i].regants[j].itemID})
      end
    end
  end
  -- print("regantItems", IWonew.TableToString(regantItems))
end



function ScanEnchantingProfit()
  regantItems = {}
  SetRegantItems()
  local sorts = {
    {sortOrder = Enum.AuctionHouseSortOrder.Price, reverseSort = false},
  }
  local lastQueryIdx = 0
  local ticker
  ticker = C_Timer.NewTicker(1, function()
    lastQueryIdx = lastQueryIdx + 1
    if lastQueryIdx > #regantItems then
      ticker:Cancel()
      return
    end
    local itemKey = {itemID = regantItems[lastQueryIdx].itemID}
    print("开始扫描", regantItems[lastQueryIdx].name)
    C_AuctionHouse.SendSearchQuery(itemKey, sorts, false)
  end)
end

